#include "StdAfx.h"
#include "DrawHoughlines.h"


CDrawHoughlines::CDrawHoughlines(void)
{
}


CDrawHoughlines::~CDrawHoughlines(void)
{
}
BEGIN_MESSAGE_MAP(CDrawHoughlines, CDrawOpenCV)
	ON_WM_PAINT()
END_MESSAGE_MAP()



void CDrawHoughlines::DealwithFrame()
{
	// TODO: Add your command handler code here
	if(m_pFrame == NULL)
	{			
		return ;
	}
	if(m_pShow == NULL)
	{

	}
	else
	{
		cvReleaseImage( &m_pShow );
	}
	

    IplImage* src = 0;
    IplImage* dst = 0;
    //IplImage* m_pShow = 0;
    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* lines = 0;
    int i;

	if (m_pFrame->nChannels==3) {
 		src = cvCreateImage(cvGetSize(m_pFrame), IPL_DEPTH_8U, 1);
		cvCvtColor(m_pFrame, src, CV_BGR2GRAY);
	}
	else {
        src = cvCloneImage( m_pFrame );
	}
	//cvFlip(src);
     
    dst = cvCreateImage( cvGetSize(src), 8, 1 );
    m_pShow = cvCreateImage( cvGetSize(src), 8, 3 );
    
    cvCanny( src, dst, 50, 200, 3 );
    cvCvtColor( dst, m_pShow, CV_GRAY2BGR );

    lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 );
    for( i = 0; i < lines->total; i++ )
    {
        CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
        cvLine( m_pShow, line[0], line[1], CV_RGB(255,0,0), 3, CV_AA, 0 );
    }

    //cvNamedWindow( "Hough", 1 );
    //cvShowImage( "Hough", m_pShow );

    cvWaitKey(0);

    cvReleaseMemStorage(&storage );
    cvReleaseImage( &src );
    cvReleaseImage( &dst );
    //cvReleaseImage( &m_pShow );
    //cvDestroyWindow( "Hough" );

}
